package com.zengdada.dml.controller;

import com.zengdada.authority.utils.CommonUtils;
import com.zengdada.authority.utils.ExcelUtil;
import com.zengdada.authority.utils.JsonMessage;
import com.zengdada.authority.utils.mapper.Page;
import com.zengdada.authority.utils.transformdata.TransformDataBlobToBlankString;
import com.zengdada.authority.utils.transformdata.TransformDataClobToString;
import com.zengdada.dml.model.type.StaticSelect;
import com.zengdada.dml.service.PlatDmlAuthorityService;
import com.zengdada.dml.utils.DmlExecuteReturn;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static com.zengdada.authority.utils.JsonMessage.fail;
import static com.zengdada.authority.utils.JsonMessage.success;

/**
 * @Auther: 12614
 * @Contact: 1261438511@qq.com
 * @Date: 2019-02-19 15:25
 * @Description:
 */
@Api(tags = "DML平台操作接口")
@Controller
@RequestMapping("/authority/plat/dml")
public class PlatDmlAuthorityController {


    @Autowired
    @Qualifier("platDmlAuthorityServiceImpl")
    PlatDmlAuthorityService platDmlAuthorityService;

    @ApiOperation(value = "分页查询接口", notes = "该接口一定会启用分页查询（默认page=1,pageSize=10）,还可以进行排序、条件查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dmlId", value = "dml语句ID", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "分页信息", dataType = "Page", paramType = "query"),
            @ApiImplicitParam(name = "searchs", value = "查询条件（满足com.sybenya.authority.utils.PublicSearch类的JSON数组字符串）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "sorts", value = "排序信息", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "_csrf", value = "验证信息", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "selectType", value = "查询类型 #1 普通查询（不会启用显示表达式）  #2 仅查询能显示的字段", dataType = "Integer", paramType = "query"),
    })
    @PostMapping("/paging_query.do")
    @ResponseBody
    public JsonMessage pagingQuery(@RequestParam(value = "dmlId", defaultValue = "") String dmlId, HttpServletRequest request, Page page, String searchs) {
        try {
            DmlExecuteReturn query = platDmlAuthorityService.query(dmlId, request, page, searchs, null);
            List<Map<String, Object>> dataMap = query.getDataMap();
            return success(CommonUtils.depthTransformDataType(dataMap, Arrays.asList(new TransformDataBlobToBlankString(), new TransformDataClobToString())), query.getTotalRow());
        } catch (Exception e) {
            return fail(e.getMessage());
        }
    }

    @ApiOperation(value = "分页查询导出excel接口", notes = "该接口一定会启用分页查询（默认page=1,pageSize=10）,还可以进行排序、条件查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dmlId", value = "dml语句ID", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "分页信息", dataType = "Page", paramType = "query"),
            @ApiImplicitParam(name = "searchs", value = "查询条件（满足com.sybenya.authority.utils.PublicSearch类的JSON数组字符串）", dataType = "String", paramType = "query"),
//            @ApiImplicitParam(name = "sorts", value = "排序信息", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "_csrf", value = "验证信息", dataType = "String", paramType = "query"),
    })
    @PostMapping("/paging_export.do")
    public void pagingExport(@RequestParam(value = "dmlId", defaultValue = "") String dmlId, HttpServletResponse response, HttpServletRequest request, Page page, String searchs) throws Exception {
        DmlExecuteReturn query = platDmlAuthorityService.query(dmlId, request, page, searchs, StaticSelect.SelectType.export);
        ExcelUtil excleUtil = new ExcelUtil();
        List<Map<String, Object>> dataMap = query.getDataMap();
        excleUtil.exportExcel(CommonUtils.depthTransformDataType(dataMap, Arrays.asList(new TransformDataBlobToBlankString(), new TransformDataClobToString())), response.getOutputStream());
    }


    @ApiOperation(value = "通用查询接口", notes = "该接口也可以进行分页查询，可以进行排序、条件查询（不可导出excel）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dmlId", value = "dml语句ID", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "分页信息-页码", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页信息-页大小", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "searchs", value = "查询条件（满足com.sybenya.authority.utils.PublicSearch类的JSON数组字符串）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "sorts", value = "排序信息", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "_csrf", value = "验证信息", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "selectType", value = "查询类型 #1 普通查询（不会启用显示表达式）  #2 仅查询能显示的字段 #3 中文表头，仅查询能显示的字段", dataType = "Integer", paramType = "query"),

    })
    @PostMapping("/query.do")
    @ResponseBody
    public JsonMessage query(@RequestParam(value = "dmlId", defaultValue = "") String dmlId, HttpServletRequest request, String searchs) {
        try {
            DmlExecuteReturn query = platDmlAuthorityService.query(dmlId, request, null, searchs, null);
            List<Map<String, Object>> dataMap = query.getDataMap();
            return success(CommonUtils.depthTransformDataType(dataMap, Arrays.asList(new TransformDataBlobToBlankString(), new TransformDataClobToString())), query.getTotalRow());
        } catch (Exception e) {
            return fail(e.getMessage());
        }
    }

    @ApiOperation(value = "数据操作接口", notes = "可以执行增删改、语句块、方法、存储过程")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dmlId", value = "dml语句ID", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "_csrf", value = "验证信息", dataType = "String", paramType = "query"),
    })
    @PostMapping("/save.do")
    @ResponseBody
    public JsonMessage save(@RequestParam(value = "dmlId", defaultValue = "") String dmlId, HttpServletRequest request) {
        try {
            DmlExecuteReturn query = platDmlAuthorityService.save(dmlId, request);
            List<Map<String, Object>> dataMap = query.getDataMap();
            return success(CommonUtils.depthTransformDataType(dataMap, Arrays.asList(new TransformDataBlobToBlankString(), new TransformDataClobToString())));
        } catch (Exception e) {
            return fail(e.getMessage());
        }
    }

}
